<template>
  <div class="app-container">
    <el-card class="box-card" shadow="hover">
      <div slot="header" class="clearfix">
        <span class="card-title">院区管理</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="showSearch = !showSearch">
          {{ showSearch ? '收起' : '展开' }}搜索
        </el-button>
      </div>

      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" class="search-form">
        <el-form-item label="所属医院ID" prop="hospitalId">
          <el-input
            v-model="queryParams.hospitalId"
            placeholder="请输入所属医院ID"
            clearable
            prefix-icon="el-icon-hospital"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input
            v-model="queryParams.phone"
            placeholder="请输入联系电话"
            clearable
            prefix-icon="el-icon-phone"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="公交" prop="car">
          <el-input
            v-model="queryParams.car"
            placeholder="请输入公交"
            clearable
            prefix-icon="el-icon-truck"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="院区名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入院区名称"
            clearable
            prefix-icon="el-icon-office-building"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="院区地址" prop="address">
          <el-input
            v-model="queryParams.address"
            placeholder="请输入院区地址"
            clearable
            prefix-icon="el-icon-location"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery" class="search-btn">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery" class="reset-btn">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8 operation-row">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:area:add']"
            class="operation-btn"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:area:edit']"
            class="operation-btn"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:area:remove']"
            class="operation-btn"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:area:export']"
            class="operation-btn"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        :data="areaList"
        @selection-change="handleSelectionChange"
        border
        stripe
        highlight-current-row
        class="custom-table"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="院区ID" align="center" prop="id" />
        <el-table-column label="所属医院" align="center" prop="hospitalName" />
        <el-table-column label="联系电话" align="center" prop="phone" />
        <el-table-column label="公交" align="center" prop="car" />
        <el-table-column label="院区名称" align="center" prop="name" />
        <el-table-column label="院区地址" align="center" prop="address" />
        <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="scope">
            <span v-if="scope.row.status === 1">
              <el-button type="success" icon="el-icon-check" circle></el-button>
            </span>
            <span v-else>
              <el-button type="danger" icon="el-icon-close" circle></el-button>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:area:edit']"
              class="table-btn"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:area:remove']"
              class="table-btn"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
        class="pagination"
      />
    </el-card>

    <!-- 添加或修改院区对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body class="custom-dialog">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属医院ID" prop="hospitalId">
          <el-select v-model="form.hospitalId" placeholder="医院院区" class="custom-select">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" prefix-icon="el-icon-phone" />
        </el-form-item>
        <el-form-item label="公交" prop="car">
          <el-input v-model="form.car" placeholder="请输入公交" prefix-icon="el-icon-truck" />
        </el-form-item>
        <el-form-item label="院区名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入院区名称" prefix-icon="el-icon-office-building" />
        </el-form-item>
        <el-form-item label="院区地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入院区地址" prefix-icon="el-icon-location" />
        </el-form-item>
        <el-form-item label="院区地址" prop="address">
          <template>
            <Map ref="map" @updateLocation="updateLocation"/>
          </template>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" class="submit-btn">确 定</el-button>
        <el-button @click="cancel" class="cancel-btn">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listArea, getArea, delArea, addArea, updateArea} from "@/api/system/area"
import Map from "@/views/index.vue";
import {getHospitalList} from "@/api/system/hospital";


export default {
  name: "Area",
  components: {Map},
  data() {

    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // k院区表格数据
      areaList: [],
      options: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hospitalId: null,
        phone: null,
        car: null,
        name: null,
        address: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        hospitalId: [
          { required: true, message: "所属医院ID不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "院区名称不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询k院区列表 */
    getList() {
      this.loading = true
      listArea(this.queryParams).then(response => {
        this.areaList = response.rows
        this.total = response.total
        this.loading = false
      })
      getHospitalList().then(res=>{
        this.options = res.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        hospitalId: null,
        phone: null,
        car: null,
        name: null,
        address: null,
        status: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加k院区"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getArea(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改k院区"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateArea(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addArea(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除k院区编号为"' + ids + '"的数据项？').then(function() {
        return delArea(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/area/export', {
        ...this.queryParams
      }, `area_${new Date().getTime()}.xlsx`)
    },
    updateLocation(Addr) {
      console.log(Addr)
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;

  .box-card {
    border-radius: 8px;
    transition: all 0.3s;

    &:hover {
      box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
    }

    .card-title {
      font-size: 18px;
      font-weight: bold;
      color: #303133;
    }
  }

  .search-form {
    margin-bottom: 20px;
    padding: 20px;
    background: #f5f7fa;
    border-radius: 4px;
    transition: all 0.3s;

    .el-form-item {
      margin-bottom: 15px;
    }

    .search-btn, .reset-btn {
      transition: all 0.3s;

      &:hover {
        transform: translateY(-2px);
      }
    }
  }

  .operation-row {
    margin-bottom: 20px;

    .operation-btn {
      transition: all 0.3s;

      &:hover {
        transform: translateY(-2px);
      }
    }
  }

  .custom-table {
    margin-bottom: 20px;

    .table-btn {
      transition: all 0.3s;

      &:hover {
        color: #409EFF;
        transform: scale(1.1);
      }
    }
  }

  .pagination {
    text-align: right;
    margin-top: 20px;
  }

  .custom-dialog {
    .el-dialog__body {
      padding: 20px 30px;
    }

    .custom-select {
      width: 100%;
    }

    .submit-btn, .cancel-btn {
      transition: all 0.3s;

      &:hover {
        transform: translateY(-2px);
      }
    }
  }
}

// 添加动画效果
.el-table__row {
  transition: all 0.3s;

  &:hover {
    background-color: #f5f7fa !important;
  }
}

.el-button {
  &.el-button--text {
    &:hover {
      background-color: transparent;
    }
  }
}

// 响应式布局
@media screen and (max-width: 768px) {
  .search-form {
    .el-form-item {
      width: 100%;
    }
  }

  .operation-row {
    .el-col {
      margin-bottom: 10px;
    }
  }
}
</style>
